// MainFrame.java 许博4221159015
package com.njwb.Swing.MainFrame;

import com.njwb.Swing.Login.LoginController;
import com.njwb.Swing.Login.LoginService;
import com.njwb.Swing.Login.LoginView;
import com.njwb.Swing.Login.StatisticsPanel;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.ArrayList;
import java.util.List;

public class MainFrame extends JFrame {
    private JPanel leftPanel;
    private JPanel rightPanel;
    private JLabel welcomeLabel;
    private Image backgroundImage;

    // 模块面板
    private StatisticsPanel statisticsPanel;
    private JPanel billView;
    private JPanel userView;
    private JPanel supplierView;

    private String username;
    private int role;

    private final int DESIGN_WIDTH = 1000;
    private final int DESIGN_HEIGHT = 620;

    // 保存所有可缩放组件的原始位置和大小
    private List<ScalableComponent> scalableComponents = new ArrayList<>();

    public MainFrame(String username, int role,
                     JPanel billView,
                     JPanel userView,
                     JPanel supplierView) {

        this.username = username;
        this.role = role;
        this.billView = billView;
        this.userView = userView;
        this.supplierView = supplierView;

        backgroundImage = new ImageIcon(getClass().getResource("/com/njwb/images/MainFrame.png")).getImage();

        setTitle("我的系统");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(DESIGN_WIDTH, DESIGN_HEIGHT);
        setLocationRelativeTo(null);
        setLayout(null);
        setUndecorated(false);
        setResizable(true);

        // 背景 Panel
        JPanel mainPanel = new BackgroundPanel();
        mainPanel.setBounds(0, 0, DESIGN_WIDTH, DESIGN_HEIGHT);
        mainPanel.setLayout(null);
        setContentPane(mainPanel);

        // 欢迎标签
        welcomeLabel = new JLabel("欢迎 " + username + " (" + (role == 0 ? "普通员工" : "经理") + ")", JLabel.CENTER);
        welcomeLabel.setFont(new Font("宋体", Font.BOLD, 16));
        mainPanel.add(welcomeLabel);
        scalableComponents.add(new ScalableComponent(welcomeLabel, 0,0,1000,40));

        // 时间标签
        JLabel timeLabel = new JLabel("", JLabel.CENTER);
        timeLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        timeLabel.setForeground(Color.black);
        mainPanel.add(timeLabel);
        scalableComponents.add(new ScalableComponent(timeLabel, 500,40,400,30));
//许博4221159015
        Timer timer = new Timer(1000, e -> {
            java.util.Date now = new java.util.Date();
            java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
            timeLabel.setText("当前时间: " + sdf.format(now));
        });
        timer.start();

        // 左侧按钮面板
        leftPanel = new JPanel(null);
        leftPanel.setOpaque(false);
        mainPanel.add(leftPanel);
        scalableComponents.add(new ScalableComponent(leftPanel, 0, 92, 190, 425));

        // 右侧内容面板
        rightPanel = new JPanel(new CardLayout());
        rightPanel.setOpaque(false);
        mainPanel.add(rightPanel);
        scalableComponents.add(new ScalableComponent(rightPanel, 197,107,777,470));

        // 初始化模块面板
        statisticsPanel = new StatisticsPanel();
        rightPanel.add(statisticsPanel, "StatisticsPanel");
        rightPanel.add(billView, "BillPanel");
        rightPanel.add(userView, "UserPanel");
        rightPanel.add(supplierView, "SupplierPanel");

        CardLayout cl = (CardLayout) rightPanel.getLayout();
        cl.show(rightPanel, "BillPanel");

        // 控制器初始化
        new MainController(this, username, role, scalableComponents);

        // 监听窗口缩放
        addComponentListener(new ComponentAdapter() {
            @Override
            public void componentResized(ComponentEvent e) {
                resizeComponents();
            }
        });

        setVisible(true);
    }

    private void resizeComponents(){
        double scaleX = (double)getWidth()/DESIGN_WIDTH;
        double scaleY = (double)getHeight()/DESIGN_HEIGHT;

        for(ScalableComponent sc : scalableComponents){
            sc.resize(scaleX, scaleY);
        }

        // 左侧按钮也要在controller里保存原始坐标，resize会在controller里触发
    }

    public JPanel getLeftPanel() { return leftPanel; }
    public JPanel getRightPanel() { return rightPanel; }
    public JLabel getWelcomeLabel() { return welcomeLabel; }

    public void exitConfirm() {
        int confirm = JOptionPane.showConfirmDialog(
                this,
                "确定要退出登录吗？",
                "确认退出",
                JOptionPane.YES_NO_OPTION,
                JOptionPane.QUESTION_MESSAGE
        );
        if (confirm == JOptionPane.YES_OPTION) {
            dispose();
            LoginView loginView = new LoginView();
            LoginService loginService = new LoginService();
            new LoginController(loginView, loginService);
            loginView.setVisible(true);
        }
    }

    private class BackgroundPanel extends JPanel {
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (backgroundImage != null) {
                g.drawImage(backgroundImage, 0, 0, getWidth(), getHeight(), this);
            }
        }
    }

    // 内部类：保存原始坐标和尺寸
    public static class ScalableComponent{
        private JComponent component;
        private int origX, origY, origWidth, origHeight;

        public ScalableComponent(JComponent component, int x, int y, int w, int h){
            this.component = component;
            this.origX = x;
            this.origY = y;
            this.origWidth = w;
            this.origHeight = h;
        }

        public void resize(double scaleX, double scaleY){
            component.setBounds((int)(origX*scaleX), (int)(origY*scaleY),
                    (int)(origWidth*scaleX), (int)(origHeight*scaleY));
        }
    }
}
